//
//  ViewController.m
//  04-nsurlsession01-get和post
//
//  Created by J on 16/2/26.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];



}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    
    [self downLoad];
}


- (void)post{
    NSURLSession* session = [NSURLSession sharedSession];//获得NSURLSession对象
    NSMutableURLRequest* mRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42"]];
    mRequest.HTTPMethod = @"POST";
    mRequest.HTTPBody = [@"username = jzj" dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSessionDataTask* task =   [session dataTaskWithRequest:mRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);

        
    }];
    [task resume];
}



- (void)downLoad{//get  NSURLSession基于CFNetWorking 自动下载到temp
    NSURLSession* session = [NSURLSession sharedSession];//获得NSURLSession对象
   NSURLSessionDownloadTask* downLoad = [session downloadTaskWithURL:[NSURL URLWithString:@"http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4"] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {//大文件自己会做处理我们只要挪过去就行了
       //将来要存放的文件真实路径
       NSString* file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:response.suggestedFilename];//服务器给的文件名 104527c7dbbb3d3jjkckcb.jpg
       
       
       UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
       NSLog(@"%@",image);
       
       UIImageView* imageView = [[UIImageView alloc]initWithImage:image];
       dispatch_async(dispatch_get_main_queue(), ^{//下载会被放到分线程中
           imageView.center = self.view.center;
//           imageView.backgroundColor = [UIColor orangeColor];
           NSLog(@"%@",imageView.image);
           [self.view addSubview:imageView];
           
       });
      NSFileManager* manager = [NSFileManager defaultManager];
       [manager moveItemAtURL:location toURL:[NSURL fileURLWithPath:file] error:nil];
       
    }];
    [downLoad resume];
  
    
    
    
    
    
    
}


- (void)get2{
    
    NSURLSession* session = [NSURLSession sharedSession];//获得NSURLSession对象
    
    NSURLSessionDataTask* task =   [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
        
        
    }];
    [task resume];
    
    
}
- (void)get{
    NSURLSession* session = [NSURLSession sharedSession];//获得NSURLSession对象
    
    NSURLSessionDataTask* task =   [session dataTaskWithURL:[NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);

        
    }];
    
    [task resume];
    
}

@end
